package com.lvmama.pangolin.controller;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lvmama.pangolin.entity.mt.MTTicketCommodityItem;
import com.lvmama.pangolin.entity.mt.MTTicketProduct;
import com.lvmama.pangolin.mt.service.MTTicketCommodityItemsService;
import com.lvmama.pangolin.mt.service.MTTicketProductService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.lvmama.pangolin.HBaseQueryPrice.HBaseTicketHistoryPrice;
import com.lvmama.pangolin.analysis.ProductParamDataAnalysis;
import com.lvmama.pangolin.busi.config.ConfigManager;
import com.lvmama.pangolin.common.Constant;
import com.lvmama.pangolin.common.DateUtil;
import com.lvmama.pangolin.common.StringTools;
import com.lvmama.pangolin.entity.City_Model;
import com.lvmama.pangolin.entity.CommodityConditionVO;
import com.lvmama.pangolin.entity.LineDataVO;
import com.lvmama.pangolin.entity.ProductConditionVO;
import com.lvmama.pangolin.entity.RelevanceProduct;
import com.lvmama.pangolin.entity.lmm.LMMTicketCommodity;
import com.lvmama.pangolin.entity.lmm.LMMTicketProduct;
import com.lvmama.pangolin.entity.qnr.QNRTicketCommodityItem;
import com.lvmama.pangolin.entity.qnr.QNRTicketProduct;
import com.lvmama.pangolin.entity.tc.TCTicketProduct;
import com.lvmama.pangolin.entity.tc.TCommodityItem;
import com.lvmama.pangolin.entity.tn.TNTicketCommodityItem;
import com.lvmama.pangolin.entity.tn.TNTicketProduct;
import com.lvmama.pangolin.entity.xc.XCTicketCommodityItem;
import com.lvmama.pangolin.entity.xc.XCTicketProduct;
import com.lvmama.pangolin.lmm.service.LMMTicketCommodityService;
import com.lvmama.pangolin.lmm.service.LMMTicketProductService;
import com.lvmama.pangolin.qnr.service.QNRTicketCommodityItemsService;
import com.lvmama.pangolin.qnr.service.QNRTicketProductService;
import com.lvmama.pangolin.service.CityService;
import com.lvmama.pangolin.service.RelevanceProductService;
import com.lvmama.pangolin.tc.service.TCCommodityItemsService;
import com.lvmama.pangolin.tc.service.TCTicketProductService;
import com.lvmama.pangolin.tn.service.TNTicketCommodityItemsService;
import com.lvmama.pangolin.tn.service.TNTicketProductService;
import com.lvmama.pangolin.xc.service.XCTicketCommodityItemsService;
import com.lvmama.pangolin.xc.service.XCTicketProductService;

import net.sf.json.JSONArray;
@Controller
@RequestMapping("/datashow")
public class ScreenDemoController {
	@Autowired
	private RelevanceProductService relevanceProductService;
	@Autowired
	private LMMTicketProductService lProductService;
	@Autowired
	private XCTicketProductService xcProductService;
	@Autowired
	private TNTicketProductService tnProductService;
	@Autowired
	private TCTicketProductService tcProductService;
	@Autowired
	private QNRTicketProductService qnrProductService;
	//新增美团
	@Autowired
	private MTTicketProductService mtProductService;
	@Autowired
	private MTTicketCommodityItemsService mtCommodityService;

	@Autowired
	private LMMTicketCommodityService lmmCommodityService;
	@Autowired
	private XCTicketCommodityItemsService xcCommodityService;
	@Autowired
	private TNTicketCommodityItemsService tnCommodityService;
	@Autowired
	private TCCommodityItemsService tcCommodityService;
	@Autowired
	private QNRTicketCommodityItemsService qnrCommodityService;
	@Autowired
	private HBaseTicketHistoryPrice historyPrice;
	@Autowired
	private CityService cityService;

	@RequestMapping("/screenDemo")
	public String screenDemo(ModelMap model,String productId,String prodPageNum,ProductConditionVO pc,String productIdVO,String businessCode){
		model.addAttribute("prodPageNum",prodPageNum);//跳转时使用
		pc.setProductId(productIdVO);
		CommodityConditionVO comm=getComm();
		comm.setProductId(productId);
		RelevanceProduct  rp=relevanceProductService.viewRelevance(productId,businessCode);//根据产品ID，业务编码查询产品关联表
		LMMTicketProduct lp=lProductService.selectByProductId(productId);//查询驴妈妈产品表
		City_Model city=cityService.queryCityById(lp.getCityCode());
		model.addAttribute("productConditionVO", pc);
		model.addAttribute("lp",lp);
		model.addAttribute("cityName",city.getName());
		/**
		 * 产品历史价格、图片丰富度、商品丰富度、商品打包率、商品优惠率、商品类型丰富度
		 */
		/**
		 * 根据productId拿到lmm历史列表并进行排序
		 */
		String tableNamel=Constant.TICKET_PRODUCT_H_LMM;
		String family=Constant.HB_VARIABLE_FAMILY;
		
		List<Map<String,String>> lmmList=historyPrice.queryByProductId(rp.getProdId(), tableNamel,family,lp.getNewGrabTime());
		Collections.sort(lmmList,new Comparator<Map<String,String>>() {
			@Override
			public int compare(Map<String, String> o1, Map<String, String> o2) {
				if(o1.get("insert_time")!=null&&o2.get("insert_time")!=null){
					if(o2==null||StringUtils.isEmpty("insert_time"))
						return -1;
					if(o1==null||StringUtils.isEmpty("insert_time"))
						return 1;
					return o2.get("insert_time").compareTo(o1.get("insert_time"));
				}else{
					return 0;
				}
				
			}
			
		});
		DecimalFormat    df   = new DecimalFormat("######0.0");
		/**年季度统计数据
		 * 
		 * 首先拿到最新抓取的时间
		 */
		Date newGrabTime=lp.getNewGrabTime();

		/**
		 * 获得去年的今天
		 */
		Date lastYear=DateUtil.getDateBeforeYear(newGrabTime);
		/**
		 * 在所有数据中获得一年的数据
		 */
		Map<String,Double> lmmStatistic=new HashMap<String,Double>();
		double max=0;
		double min=Double.MAX_VALUE;
		double avg=0;
		int len=0;
		for(int i=0;i<lmmList.size();i++){
			Map<String, String> lmm=lmmList.get(i);
			
			Date insertTime=DateUtil.stringToDate(lmm.get("insert_time"),"yyyyMMdd");
			
			if(lmm.get("lowest_price")!=null&&lmm.get("lowest_price").length()>0&&StringTools.isDoubleNumeric(lmm.get("lowest_price"))){
				if(DateUtil.compare_date(insertTime, lastYear)>0){//代表一年内
					if(Double.parseDouble(lmm.get("lowest_price"))<min){
						min=Double.parseDouble(lmm.get("lowest_price"));
					}
					if(Double.parseDouble(lmm.get("lowest_price"))>max){
						max=Double.parseDouble(lmm.get("lowest_price"));
					}
					avg+=Double.parseDouble(lmm.get("lowest_price"));
					len+=1;
				}else if(DateUtil.compare_date(insertTime, lastYear)==0){
					avg+=Double.parseDouble(lmm.get("lowest_price"));
					len+=1;
					lmmStatistic.put("lmmYearSame", Double.parseDouble(lmm.get("lowest_price")));
				}
				
			}
		
		}
		lmmStatistic.put("lmmYearMax",max );
		if(min!=Double.MAX_VALUE){
			lmmStatistic.put("lmmYearMin", min);
		}
		
		if(len!=0){
			lmmStatistic.put("lmmYearAvg", Double.parseDouble(df.format(avg/len)));
		}else{
			lmmStatistic.put("lmmYearAvg", Double.parseDouble(df.format(0)));
		}
		
		/**
		 * 上个季度的今天（前3个月）
		 */
		Date lastQuarter=DateUtil.getDateBeforeQuarter(newGrabTime);
		/**
		 * 在所有数据中得到上个季度的数据
		 */
		double max1=0;
		double min1=Double.MAX_VALUE;
		double avg1=0;
		int len1=0;
		for(int i=0;i<lmmList.size();i++){
			Map<String, String> lmm=lmmList.get(i);
			Date insertTime=DateUtil.stringToDate(lmm.get("insert_time"),"yyyyMMdd");
			if(lmm.get("lowest_price")!=null&&lmm.get("lowest_price").length()>0&&StringTools.isDoubleNumeric(lmm.get("lowest_price"))){
				if(DateUtil.compare_date(insertTime, lastQuarter)>0){
					if(Double.parseDouble(lmm.get("lowest_price"))<min1){
						min1=Double.parseDouble(lmm.get("lowest_price"));
					}
					if(Double.parseDouble(lmm.get("lowest_price"))>max1){
						max1=Double.parseDouble(lmm.get("lowest_price"));
					}
					avg1+=Double.parseDouble(lmm.get("lowest_price"));
					len1+=1;
				}else if(DateUtil.compare_date(insertTime, lastQuarter)==0){
					avg1+=Double.parseDouble(lmm.get("lowest_price"));
					len1+=1;
					lmmStatistic.put("lmmQuarterSame", Double.parseDouble(lmm.get("lowest_price")));
				}
			}
		}
		lmmStatistic.put("lmmQuarterMax",max1 );
		if(min1!=Double.MAX_VALUE){
			lmmStatistic.put("lmmQuarterMin", min1);
		}
		if(len1!=0){
			lmmStatistic.put("lmmQuarterAvg", Double.parseDouble(df.format(avg1/len1)));
		}else{
			lmmStatistic.put("lmmQuarterAvg", Double.parseDouble(df.format(avg1)));
		}
		
		/**
		 * 上个月的今天
		 */
		Date lastMonth=DateUtil.getDateBeforeMonth(newGrabTime);
		/**
		 * 在所有数据中得到上个月的数据
		 */
		double max2=0;
		double min2=Double.MAX_VALUE;
		double avg2=0;
		int len2=0;
		for(int i=0;i<lmmList.size();i++){
			Map<String, String> lmm=lmmList.get(i);
			Date insertTime=DateUtil.stringToDate(lmm.get("insert_time"),"yyyyMMdd");
			if(lmm.get("lowest_price")!=null&&lmm.get("lowest_price").length()>0&&StringTools.isDoubleNumeric(lmm.get("lowest_price"))){
				if(DateUtil.compare_date(insertTime, lastMonth)>0){
					if(Double.parseDouble(lmm.get("lowest_price"))<min2){
						min2=Double.parseDouble(lmm.get("lowest_price"));
					}
					if(Double.parseDouble(lmm.get("lowest_price"))>max2){
						max2=Double.parseDouble(lmm.get("lowest_price"));
					}
					avg2+=Double.parseDouble(lmm.get("lowest_price"));
					len2+=1;
				}else if(DateUtil.compare_date(insertTime, lastMonth)==0){
					avg2+=Double.parseDouble(lmm.get("lowest_price"));
					len2+=1;
					lmmStatistic.put("lmmMonthSame", Double.parseDouble(lmm.get("lowest_price")));
				}
			}
		}
		lmmStatistic.put("lmmMonthMax",max2 );
		if(min2!=Double.MAX_VALUE){
			lmmStatistic.put("lmmMonthMin", min2);
		}
		if(len2!=0){
			lmmStatistic.put("lmmMonthAvg", Double.parseDouble(df.format(avg2/len2)));
		}else{
			lmmStatistic.put("lmmMonthAvg", Double.parseDouble(df.format(avg2)));
		}
		
		XCTicketProduct xcp=null;
		if(rp.getProdIdXC()!=null&&!"".equals(rp.getProdIdXC().trim())){
			xcp=xcProductService.selectByProdId(rp.getProdIdXC());
			model.addAttribute("xcp",xcp);
			if(xcp!=null){
				/**
				 * 根据productId拿到xc历史列表并进行排序
				 */
				String tableNamex=Constant.TICKET_PRODUCT_H_XC;
				List<Map<String,String>> xcList=historyPrice.queryByProductId(rp.getProdIdXC(), tableNamex,family,xcp.getCreateTime());
				if(xcList!=null){
					Collections.sort(xcList,new Comparator<Map<String,String>>() {
						@Override
						public int compare(Map<String, String> o1, Map<String, String> o2) {
							if(o2==null||StringUtils.isEmpty(o2.get("create_time")))
								return -1;
							if(o1==null||StringUtils.isEmpty(o1.get("create_time")))
								return 1;
							return o2.get("create_time").compareTo(o1.get("create_time"));
						}
						
					});
					
					/**
					 * 年季度统计值
					 */
					Date xcNewGrabTime=xcp.getCreateTime();

					/**
					 * 获得去年的今天
					 */
					Date xcLastYear=DateUtil.getDateBeforeYear(xcNewGrabTime);
					/**
					 * 在所有数据中获得一年的数据
					 */
					
					max=0;
					min=Double.MAX_VALUE;
					avg=0;
					len=0;
					for(int i=0;i<xcList.size();i++){
						Map<String, String> xc=xcList.get(i);
						Date insertTime=DateUtil.stringToDate(xc.get("create_time"),"yyyyMMdd");
						if(xc.get("lowest_price")!=null&&xc.get("lowest_price").length()>0&&StringTools.isDoubleNumeric(xc.get("lowest_price"))){
							if(DateUtil.compare_date(insertTime, xcLastYear)>0){
								if(Double.parseDouble(xc.get("lowest_price"))<min){
									min=Double.parseDouble(xc.get("lowest_price"));
								}
								if(Double.parseDouble(xc.get("lowest_price"))>max){
									max=Double.parseDouble(xc.get("lowest_price"));
								}
								avg+=Double.parseDouble(xc.get("lowest_price"));
								len+=1;
							}else if(DateUtil.compare_date(insertTime, xcLastYear)==0){
								lmmStatistic.put("xcYearSame", Double.parseDouble(xc.get("lowest_price")));
								avg+=Double.parseDouble(xc.get("lowest_price"));
								len+=1;
							}
						}
					}
					lmmStatistic.put("xcYearMax",max );
					if(min!=Double.MAX_VALUE){
						lmmStatistic.put("xcYearMin", min);
					}
					
					if(len!=0){
						lmmStatistic.put("xcYearAvg", Double.parseDouble(df.format(avg/len)));
					}else{
						lmmStatistic.put("xcYearAvg", Double.parseDouble(df.format(0)));
					}
					
					/**
					 * 上个季度的今天（前3个月）
					 */
					Date xcLastQuarter=DateUtil.getDateBeforeQuarter(xcNewGrabTime);
					/**
					 * 在所有数据中得到上个季度的数据
					 */
					max=0;
					min=Double.MAX_VALUE;
					avg=0;
					len=0;
					for(int i=0;i<xcList.size();i++){
						Map<String, String> xc=xcList.get(i);
						Date insertTime=DateUtil.stringToDate(xc.get("create_time"),"yyyyMMdd");
						if(xc.get("lowest_price")!=null&&xc.get("lowest_price").length()>0&&StringTools.isDoubleNumeric(xc.get("lowest_price"))){
							if(DateUtil.compare_date(insertTime, xcLastQuarter)>0){
								if(Double.parseDouble(xc.get("lowest_price"))<min){
									min=Double.parseDouble(xc.get("lowest_price"));
								}
								if(Double.parseDouble(xc.get("lowest_price"))>max){
									max=Double.parseDouble(xc.get("lowest_price"));
								}
								avg+=Double.parseDouble(xc.get("lowest_price"));
								len+=1;
							}else if(DateUtil.compare_date(insertTime, xcLastQuarter)==0){
								avg+=Double.parseDouble(xc.get("lowest_price"));
								len+=1;
								lmmStatistic.put("xcQuarterSame", Double.parseDouble(xc.get("lowest_price")));
							}
						}
					}
					lmmStatistic.put("xcQuarterMax",max );
					if(min!=Double.MAX_VALUE){
						lmmStatistic.put("xcQuarterMin", min);
					}
					
					if(len!=0){
						lmmStatistic.put("xcQuarterAvg", Double.parseDouble(df.format(avg/len)));
					}else{
						lmmStatistic.put("xcQuarterAvg",Double.parseDouble(df.format(0)));
					}
					
					/**
					 * 上个月的今天
					 */
					Date xcLastMonth=DateUtil.getDateBeforeMonth(xcNewGrabTime);
					/**
					 * 在所有数据中得到上个月的数据
					 */
					max=0;
					min=Double.MAX_VALUE;
					avg=0;
					len=0;
					for(int i=0;i<xcList.size();i++){
						Map<String, String> xc=xcList.get(i);
						Date insertTime=DateUtil.stringToDate(xc.get("create_time"),"yyyyMMdd");
						if(xc.get("lowest_price")!=null&&xc.get("lowest_price").length()>0){
							if(DateUtil.compare_date(insertTime, xcLastMonth)>0&&StringTools.isDoubleNumeric(xc.get("lowest_price"))){
								if(Double.parseDouble(xc.get("lowest_price"))<min){
									min=Double.parseDouble(xc.get("lowest_price"));
								}
								if(Double.parseDouble(xc.get("lowest_price"))>max){
									max=Double.parseDouble(xc.get("lowest_price"));
								}
								avg+=Double.parseDouble(xc.get("lowest_price"));
								len+=1;
							}else if(DateUtil.compare_date(insertTime, xcLastMonth)==0){
								avg+=Double.parseDouble(xc.get("lowest_price"));
								len+=1;
								lmmStatistic.put("xcMonthSame", Double.parseDouble(xc.get("lowest_price")));
							}
						}
					}
					
					lmmStatistic.put("xcMonthMax",max );
					if(min!=Double.MAX_VALUE){
						lmmStatistic.put("xcMonthMin", min);
					}
					
					if(len!=0){
						lmmStatistic.put("xcMonthAvg", Double.parseDouble(df.format(avg/len)));	
					}else{
						lmmStatistic.put("xcMonthAvg",Double.parseDouble(df.format(0)));
					}
							
				}
			}
			
		}
		TNTicketProduct tnp=null;
		if(rp.getProdIdTN()!=null&&!"".equals(rp.getProdIdTN().trim())){
			tnp=tnProductService.selectByProdId(rp.getProdIdTN());
			model.addAttribute("tnp",tnp);
			if(tnp!=null){
				/**
				 * 根据productId拿到tn历史列表并进行排序
				 */
				String tableNamen=Constant.TICKET_PRODUCT_H_TN;
				List<Map<String,String>> tnList=historyPrice.queryByProductId(rp.getProdIdTN(), tableNamen,family,tnp.getCreateTime());
				if(tnList!=null){
					Collections.sort(tnList,new Comparator<Map<String,String>>() {
						@Override
						public int compare(Map<String, String> o1, Map<String, String> o2) {
							if(o2==null||StringUtils.isEmpty("create_time"))
								return -1;
							if(o1==null||StringUtils.isEmpty(o1.get("create_time")))
								return 1;
							return o2.get("create_time").compareTo(o1.get("create_time"));
						}
						
					});
					model.addAttribute("tnList", tnList);
					/**
					 * 年季度统计值
					 */
					Date tnNewGrabTime=tnp.getCreateTime();

					/**
					 * 获得去年的今天
					 */
					Date tnLastYear=DateUtil.getDateBeforeYear(tnNewGrabTime);
					/**
					 * 在所有数据中获得一年的数据
					 */
					
					max=0;
					min=Double.MAX_VALUE;
					avg=0;
					len=0;
					for(int i=0;i<tnList.size();i++){
						Map<String, String> tn=tnList.get(i);
						if(tn.get("create_time")!=null&&!"".equals(tn.get("create_time"))){
							String data=tn.get("create_time");
							Date insertTime=DateUtil.stringToDate(data,"yyyyMMdd");
							if(tn.get("lowest_price")!=null&&tn.get("lowest_price").length()>0&&StringTools.isDoubleNumeric(tn.get("lowest_price"))){
								if(DateUtil.compare_date(insertTime, tnLastYear)>0){
									if(Double.parseDouble(tn.get("lowest_price"))<min){
										min=Double.parseDouble(tn.get("lowest_price"));
									}
									if(Double.parseDouble(tn.get("lowest_price"))>max){
										max=Double.parseDouble(tn.get("lowest_price"));
									}
									avg+=Double.parseDouble(tn.get("lowest_price"));
									len+=1;
								}else if(DateUtil.compare_date(insertTime, tnLastYear)==0){
									avg+=Double.parseDouble(tn.get("lowest_price"));
									len+=1;
									lmmStatistic.put("tnYearSame", Double.parseDouble(tn.get("lowest_price")));
								}
						}
						
						}
					}
					lmmStatistic.put("tnYearMax",max );
					if(min!=Double.MAX_VALUE){
						lmmStatistic.put("tnYearMin", min);
					}
					
					if(len!=0){
						lmmStatistic.put("tnYearAvg", Double.parseDouble(df.format(avg/len)));
					}else{
						lmmStatistic.put("tnYearAvg",Double.parseDouble(df.format(0)));
					}
					
					/**
					 * 上个季度的今天（前3个月）
					 */
					Date tnLastQuarter=DateUtil.getDateBeforeQuarter(tnNewGrabTime);
					/**
					 * 在所有数据中得到上个季度的数据
					 */
					max=0;
					min=Double.MAX_VALUE;
					avg=0;
					len=0;
					for(int i=0;i<tnList.size();i++){
						Map<String, String> tn=tnList.get(i);
						Date insertTime=DateUtil.stringToDate(tn.get("create_time"),"yyyyMMdd");
						if(tn.get("lowest_price")!=null&&tn.get("lowest_price").length()>0&&StringTools.isDoubleNumeric(tn.get("lowest_price"))){
							if(DateUtil.compare_date(insertTime, tnLastQuarter)>0){
								if(Double.parseDouble(tn.get("lowest_price"))<min){
									min=Double.parseDouble(tn.get("lowest_price"));
								}
								if(Double.parseDouble(tn.get("lowest_price"))>max){
									max=Double.parseDouble(tn.get("lowest_price"));
								}
								avg+=Double.parseDouble(tn.get("lowest_price"));
								len+=1;
							}else if(DateUtil.compare_date(insertTime, tnLastQuarter)==0){
								avg+=Double.parseDouble(tn.get("lowest_price"));
								len+=1;
								lmmStatistic.put("tnQuarterSame", Double.parseDouble(tn.get("lowest_price")));
							}
						}
					}
					lmmStatistic.put("tnQuarterMax",max );
					if(min!=Double.MAX_VALUE){
						lmmStatistic.put("tnQuarterMin", min);
					}
					
					if(len!=0){
						lmmStatistic.put("tnQuarterAvg", Double.parseDouble(df.format(avg/len)));
					}else{
						lmmStatistic.put("tnQuarterAvg",Double.parseDouble(df.format(0)));
					}
					
					/**
					 * 上个月的今天
					 */
					Date tnLastMonth=DateUtil.getDateBeforeMonth(tnNewGrabTime);
					/**
					 * 在所有数据中得到上个月的数据
					 */
					max=0;
					min=Double.MAX_VALUE;
					avg=0;
					len=0;
					for(int i=0;i<tnList.size();i++){
						Map<String, String> tn=tnList.get(i);
						Date insertTime=DateUtil.stringToDate(tn.get("create_time"),"yyyyMMdd");
						if(tn.get("lowest_price")!=null&&tn.get("lowest_price").length()>0&&StringTools.isDoubleNumeric(tn.get("lowest_price"))){
							if(DateUtil.compare_date(insertTime, tnLastMonth)>0){
								if(Double.parseDouble(tn.get("lowest_price"))<min){
									min=Double.parseDouble(tn.get("lowest_price"));
								}
								if(Double.parseDouble(tn.get("lowest_price"))>max){
									max=Double.parseDouble(tn.get("lowest_price"));
								}
								avg+=Double.parseDouble(tn.get("lowest_price"));
								len+=1;
							}else if(DateUtil.compare_date(insertTime, tnLastMonth)==0){
								avg+=Double.parseDouble(tn.get("lowest_price"));
								len+=1;
								lmmStatistic.put("tnMonthSame", Double.parseDouble(tn.get("lowest_price")));
							}
						}
					}
					lmmStatistic.put("tnMonthMax",max );
					if(min!=Double.MAX_VALUE){
						lmmStatistic.put("tnMonthMin", min);
					}
					
					if(len!=0){
						lmmStatistic.put("tnMonthAvg", Double.parseDouble(df.format(avg/len)));
					}else{
						lmmStatistic.put("tnMonthAvg", Double.parseDouble(df.format(0)));
					}
					
				}
			}
			
		}
		TCTicketProduct tcp=null;
		if(rp.getProdIdTC()!=null&&!"".equals(rp.getProdIdTC().trim())){
			tcp=tcProductService.selectByProdId(rp.getProdIdTC());
			model.addAttribute("tcp",tcp);
			if(tcp!=null){
				/**
				 * 根据productId拿到tc历史列表并进行排序
				 */
				String tableNamec=Constant.TICKET_PRODUCT_H_TC;
				List<Map<String,String>> tcList=historyPrice.queryByProductId(rp.getProdIdTC(), tableNamec,family,tcp.getNewGrabTime());
				if(tcList!=null){
					Collections.sort(tcList,new Comparator<Map<String,String>>() {
						@Override
						public int compare(Map<String, String> o1, Map<String, String> o2) {
							if(o2==null||StringUtils.isEmpty(o2.get("create_time")))
								return -1;
							if(o1==null||StringUtils.isEmpty(o1.get("create_time")))
								return 1;
							return o2.get("create_time").compareTo(o1.get("create_time"));
						}
						
					});
					model.addAttribute("tcList", tcList);
					
					/**
					 * 年季度统计值
					 */
					Date tcNewGrabTime=tcp.getNewGrabTime();

					/**
					 * 获得去年的今天
					 */
					Date xcLastYear=DateUtil.getDateBeforeYear(tcNewGrabTime);
					/**
					 * 在所有数据中获得一年的数据
					 */
					
					max=0;
					min=Double.MAX_VALUE;
					avg=0;
					len=0;
					for(int i=0;i<tcList.size();i++){
						Map<String, String> tc=tcList.get(i);
						Date insertTime=DateUtil.stringToDate(tc.get("create_time"),"yyyyMMdd");
						if(tc.get("lowest_price")!=null&&tc.get("lowest_price").length()>0&&StringTools.isDoubleNumeric(tc.get("lowest_price"))){
							if(DateUtil.compare_date(insertTime, xcLastYear)>0){
								if(Double.parseDouble(tc.get("lowest_price"))<min){
									min=Double.parseDouble(tc.get("lowest_price"));
								}
								if(Double.parseDouble(tc.get("lowest_price"))>max){
									max=Double.parseDouble(tc.get("lowest_price"));
								}
								avg+=Double.parseDouble(tc.get("lowest_price"));
								len+=1;
							}else if(DateUtil.compare_date(insertTime, xcLastYear)==0){
								avg+=Double.parseDouble(tc.get("lowest_price"));
								len+=1;
								lmmStatistic.put("tcYearSame", Double.parseDouble(tc.get("lowest_price")));
							}
						}
					}
					lmmStatistic.put("tcYearMax",max );
					if(min!=Double.MAX_VALUE){
						lmmStatistic.put("tcYearMin", min);
					}
					
					if(len!=0){
						lmmStatistic.put("tcYearAvg", Double.parseDouble(df.format(avg/len)));
					}else{
						lmmStatistic.put("tcYearAvg", Double.parseDouble(df.format(0)));
					}
					
					/**
					 * 上个季度的今天（前3个月）
					 */
					Date tcLastQuarter=DateUtil.getDateBeforeQuarter(tcNewGrabTime);
					/**
					 * 在所有数据中得到上个季度的数据
					 */
					max=0;
					min=Double.MAX_VALUE;
					avg=0;
					len=0;
					for(int i=0;i<tcList.size();i++){
						Map<String, String> tc=tcList.get(i);
						Date insertTime=DateUtil.stringToDate(tc.get("create_time"),"yyyyMMdd");
						if(tc.get("lowest_price")!=null&&tc.get("lowest_price").length()>0&&StringTools.isDoubleNumeric(tc.get("lowest_price"))){
							if(DateUtil.compare_date(insertTime, tcLastQuarter)>0){
								if(Double.parseDouble(tc.get("lowest_price"))<min){
									min=Double.parseDouble(tc.get("lowest_price"));
								}
								if(Double.parseDouble(tc.get("lowest_price"))>max){
									max=Double.parseDouble(tc.get("lowest_price"));
								}
								avg+=Double.parseDouble(tc.get("lowest_price"));
								len+=1;
							}else if(DateUtil.compare_date(insertTime, tcLastQuarter)==0){
								avg+=Double.parseDouble(tc.get("lowest_price"));
								len+=1;
								lmmStatistic.put("tcQuarterSame", Double.parseDouble(tc.get("lowest_price")));
							}
						}
					}
					lmmStatistic.put("tcQuarterMax",max );
					if(min!=Double.MAX_VALUE){
						lmmStatistic.put("tcQuarterMin", min);
					}
					
					if(len!=0){
						lmmStatistic.put("tcQuarterAvg", Double.parseDouble(df.format(avg/len)));
					}else{
						lmmStatistic.put("tcQuarterAvg", Double.parseDouble(df.format(0)));
					}
					
					/**
					 * 上个月的今天
					 */
					Date tcLastMonth=DateUtil.getDateBeforeMonth(tcNewGrabTime);
					/**
					 * 在所有数据中得到上个月的数据
					 */
					max=0;
					min=Double.MAX_VALUE;
					avg=0;
					len=0;
					for(int i=0;i<tcList.size();i++){
						Map<String, String> tc=tcList.get(i);
						Date insertTime=DateUtil.stringToDate(tc.get("create_time"),"yyyyMMdd");
						if(tc.get("lowest_price")!=null&&tc.get("lowest_price").length()>0&&StringTools.isDoubleNumeric(tc.get("lowest_price"))){
							if(DateUtil.compare_date(insertTime, tcLastMonth)>0){
								if(Double.parseDouble(tc.get("lowest_price"))<min){
									min=Double.parseDouble(tc.get("lowest_price"));
								}
								if(Double.parseDouble(tc.get("lowest_price"))>max){
									max=Double.parseDouble(tc.get("lowest_price"));
								}
								avg+=Double.parseDouble(tc.get("lowest_price"));
								len+=1;
							}else if(DateUtil.compare_date(insertTime, tcLastMonth)==0){
								avg+=Double.parseDouble(tc.get("lowest_price"));
								len+=1;
								lmmStatistic.put("tcMonthSame", Double.parseDouble(tc.get("lowest_price")));
							}
						}
					}
					lmmStatistic.put("tcMonthMax",max );
					if(min!=Double.MAX_VALUE){
						lmmStatistic.put("tcMonthMin", min);
					}
					
					if(len!=0){
						lmmStatistic.put("tcMonthAvg", Double.parseDouble(df.format(avg/len)));
					}else{
						lmmStatistic.put("tcMonthAvg", Double.parseDouble(df.format(0)));
					}
					
				}
			}
			
		}
		QNRTicketProduct qnrp=new QNRTicketProduct();
		if(rp.getProdIdQNR()!=null&&!"".equals(rp.getProdIdQNR().trim())){
			qnrp=qnrProductService.selectByProdId(rp.getProdIdQNR());
			model.addAttribute("qnrp",qnrp);
			if(qnrp!=null){
				/**
				 * 根据productId拿到qnr历史列表并进行排序
				 */
				String tableNameq=Constant.TICKET_PRODUCT_H_QNR;
				List<Map<String,String>> qnrList=historyPrice.queryByProductId(rp.getProdIdQNR(), tableNameq,family,qnrp.getCreateTime());
				if(qnrList!=null){
					Collections.sort(qnrList,new Comparator<Map<String,String>>() {
						@Override
						public int compare(Map<String, String> o1, Map<String, String> o2) {
							if(o2==null||StringUtils.isEmpty(o2.get("create_time")))
								return -1;
							if(o1==null||StringUtils.isEmpty(o1.get("create_time")))
								return 1;
							return o2.get("create_time").compareTo(o1.get("create_time"));
						}
						
					});
					model.addAttribute("qnrList", qnrList);
					
					/**
					 * 年季度统计值
					 */
					Date qnrNewGrabTime=qnrp.getCreateTime();

					/**
					 * 获得去年的今天
					 */
					Date qnrLastYear=DateUtil.getDateBeforeYear(qnrNewGrabTime);
					/**
					 * 在所有数据中获得一年的数据
					 */
					max=0;
					min=Double.MAX_VALUE;
					avg=0;
					len=0;
					for(int i=0;i<qnrList.size();i++){
						Map<String, String> qnr=qnrList.get(i);
						Date insertTime=DateUtil.stringToDate(qnr.get("create_time"),"yyyyMMdd");
						if(qnr.get("lowest_price")!=null&&qnr.get("lowest_price").length()>0&&StringTools.isDoubleNumeric(qnr.get("lowest_price"))){
							if(DateUtil.compare_date(insertTime, qnrLastYear)>0){
								if(Double.parseDouble(qnr.get("lowest_price"))<min){
									min=Double.parseDouble(qnr.get("lowest_price"));
								}
								if(Double.parseDouble(qnr.get("lowest_price"))>max){
									max=Double.parseDouble(qnr.get("lowest_price"));
								}
								avg+=Double.parseDouble(qnr.get("lowest_price"));
								len+=1;
							}else if(DateUtil.compare_date(insertTime, qnrLastYear)==0){
								avg+=Double.parseDouble(qnr.get("lowest_price"));
								len+=1;
								lmmStatistic.put("qnrYearSame", Double.parseDouble(qnr.get("lowest_price")));
							}
						}
					}
					   
					lmmStatistic.put("qnrYearMax",max );
					if(min!=Double.MAX_VALUE){
						lmmStatistic.put("qnrYearMin", min);
					}
					if(len!=0){
						lmmStatistic.put("qnrYearAvg", Double.parseDouble(df.format(avg/len)));
					}else{
						lmmStatistic.put("qnrYearAvg", Double.parseDouble(df.format(0)));
					}
					
					/**
					 * 上个季度的今天（前3个月）
					 */
					Date qnrLastQuarter=DateUtil.getDateBeforeQuarter(qnrNewGrabTime);
					/**
					 * 在所有数据中得到上个季度的数据
					 */
					max=0;
					min=Double.MAX_VALUE;
					avg=0;
					len=0;
					for(int i=0;i<qnrList.size();i++){
						Map<String, String> qnr=qnrList.get(i);
						Date insertTime=DateUtil.stringToDate(qnr.get("create_time"),"yyyyMMdd");
						if(qnr.get("lowest_price")!=null&&qnr.get("lowest_price").length()>0&&StringTools.isDoubleNumeric(qnr.get("lowest_price"))){
							if(DateUtil.compare_date(insertTime, qnrLastQuarter)>0){
								if(Double.parseDouble(qnr.get("lowest_price"))<min){
									min=Double.parseDouble(qnr.get("lowest_price"));
								}
								if(Double.parseDouble(qnr.get("lowest_price"))>max){
									max=Double.parseDouble(qnr.get("lowest_price"));
								}
								avg+=Double.parseDouble(qnr.get("lowest_price"));
								len+=1;
							}else if(DateUtil.compare_date(insertTime, qnrLastQuarter)==0){
								avg+=Double.parseDouble(qnr.get("lowest_price"));
								len+=1;
								lmmStatistic.put("qnrQuarterSame", Double.parseDouble(qnr.get("lowest_price")));
							}
						}
					}
					lmmStatistic.put("qnrQuarterMax",max );
					if(min!=Double.MAX_VALUE){
						lmmStatistic.put("qnrQuarterMin", min);
					}
					
					if(len!=0){
						lmmStatistic.put("qnrQuarterAvg", Double.parseDouble(df.format(avg/len)));
					}else{
						lmmStatistic.put("qnrQuarterAvg", Double.parseDouble(df.format(0)));
					}
					
					/**
					 * 上个月的今天
					 */
					Date qnrLastMonth=DateUtil.getDateBeforeMonth(qnrNewGrabTime);
					/**
					 * 在所有数据中得到上个月的数据
					 */
					max=0;
					min=Double.MAX_VALUE;
					avg=0;
					len=0;
					for(int i=0;i<qnrList.size();i++){
						Map<String, String> qnr=qnrList.get(i);
						Date insertTime=DateUtil.stringToDate(qnr.get("create_time"),"yyyyMMdd");
						if(qnr.get("lowest_price")!=null&&qnr.get("lowest_price").length()>0&&StringTools.isDoubleNumeric(qnr.get("lowest_price"))){
							if(DateUtil.compare_date(insertTime, qnrLastMonth)>0){
								if(Double.parseDouble(qnr.get("lowest_price"))<min){
									min=Double.parseDouble(qnr.get("lowest_price"));
								}
								if(Double.parseDouble(qnr.get("lowest_price"))>max){
									max=Double.parseDouble(qnr.get("lowest_price"));
								}
								avg+=Double.parseDouble(qnr.get("lowest_price"));
								len+=1;
							}else if(DateUtil.compare_date(insertTime, qnrLastMonth)==0){
								avg+=Double.parseDouble(qnr.get("lowest_price"));
								len+=1;
								lmmStatistic.put("qnrMonthSame", Double.parseDouble(qnr.get("lowest_price")));
							}
						}
					}
					lmmStatistic.put("qnrMonthMax",max );
					if(min!=Double.MAX_VALUE){
						lmmStatistic.put("qnrMonthMin", min);
					}
					
					if(len!=0){
						lmmStatistic.put("qnrMonthAvg", Double.parseDouble(df.format(avg/len)));
					}else{
						lmmStatistic.put("qnrMonthAvg", Double.parseDouble(df.format(0)));
					}
					
				}
			}
			
		}

		//新增美团 - 该处代码需要被重构
		MTTicketProduct mtp=new MTTicketProduct();
		if(rp.getProdIdMT()!=null&&!"".equals(rp.getProdIdMT().trim())){
			mtp=mtProductService.selectByProdId(rp.getProdIdMT());
			model.addAttribute("mtp",mtp);
			if(mtp!=null){
				/**
				 * 根据productId拿到mtp历史列表并进行排序
				 */
//				String tableNameq=Constant.TICKET_PRODUCT_H_QNR;//暂时使用去哪儿数据
				String tableNameq=Constant.TICKET_PRODUCT_H_MT;//查询美团历史数据
				List<Map<String,String>> mtList=historyPrice.queryByProductId(rp.getProdIdMT(), tableNameq,family,mtp.getCreateTime());
//				List<Map<String,String>> mtList=historyPrice.queryByProductId(rp.getProdIdQNR(), tableNameq,family,qnrp.getCreateTime());
				if(mtList!=null){
					Collections.sort(mtList,new Comparator<Map<String,String>>() {
						@Override
						public int compare(Map<String, String> o1, Map<String, String> o2) {
//							return o2.get("create_time").compareTo(o1.get("create_time"));
							if(o2==null||StringUtils.isEmpty(o2.get("createTime")))//排除空指针
								return -1;
							if(o1==null||StringUtils.isEmpty(o1.get("createTime")))
								return 1;
							return o2.get("createTime").compareTo(o1.get("createTime"));
						}

					});
					model.addAttribute("mtList", mtList);

					/**
					 * 年季度统计值
					 */
					Date mtNewGrabTime=mtp.getCreateTime();

					/**
					 * 获得去年的今天
					 */
					Date mtLastYear=DateUtil.getDateBeforeYear(mtNewGrabTime);
					/**
					 * 在所有数据中获得一年的数据
					 */
					max=0;
					min=Double.MAX_VALUE;
					avg=0;
					len=0;
					for(int i=0;i<mtList.size();i++){
						Map<String, String> mt=mtList.get(i);
						if(mt!=null&&mt.get("createTime")!=null){//此处程序不进入 createTime
//						if(mt!=null&&mt.get("create_time")!=null){//此处程序不进入 createTime
							Date insertTime=DateUtil.stringToDate(mt.get("createTime"),"yyyyMMdd");
							if(mt.get("lowestPrice")!=null&&mt.get("lowestPrice").length()>0&&StringTools.isDoubleNumeric(mt.get("lowestPrice"))){
//							if(mt.get("lowest_price")!=null&&mt.get("lowest_price").length()>0&&StringTools.isDoubleNumeric(mt.get("lowest_price"))){
								if(DateUtil.compare_date(insertTime, mtLastYear)>0){
									if(Double.parseDouble(mt.get("lowestPrice"))<min){
										min=Double.parseDouble(mt.get("lowestPrice"));
									}
									if(Double.parseDouble(mt.get("lowestPrice"))>max){
										max=Double.parseDouble(mt.get("lowestPrice"));
									}
									avg+=Double.parseDouble(mt.get("lowestPrice"));
									len+=1;
								}else if(DateUtil.compare_date(insertTime, mtLastYear)==0){
									avg+=Double.parseDouble(mt.get("lowestPrice"));
									len+=1;
									lmmStatistic.put("mtYearSame", Double.parseDouble(mt.get("lowestPrice")));
								}
							}
						}

					}

					lmmStatistic.put("mtYearMax",max );
					if(min!=Double.MAX_VALUE){
						lmmStatistic.put("mtYearMin", min);
					}
					if(len!=0){
						lmmStatistic.put("mtYearAvg", Double.parseDouble(df.format(avg/len)));
					}else{
						lmmStatistic.put("mtYearAvg", Double.parseDouble(df.format(0)));
					}

					/**
					 * 上个季度的今天（前3个月）
					 */
					Date mtLastQuarter=DateUtil.getDateBeforeQuarter(mtNewGrabTime);
					/**
					 * 在所有数据中得到上个季度的数据
					 */
					max=0;
					min=Double.MAX_VALUE;
					avg=0;
					len=0;
					for(int i=0;i<mtList.size();i++){
						Map<String, String> mt=mtList.get(i);
						if(mt!=null&&mt.get("createTime")!=null){
							Date insertTime=DateUtil.stringToDate(mt.get("createTime"),"yyyyMMdd");
							if(mt.get("lowestPrice")!=null&&mt.get("lowestPrice").length()>0&&StringTools.isDoubleNumeric(mt.get("lowestPrice"))){
								if(DateUtil.compare_date(insertTime, mtLastQuarter)>0){
									if(Double.parseDouble(mt.get("lowestPrice"))<min){
										min=Double.parseDouble(mt.get("lowestPrice"));
									}
									if(Double.parseDouble(mt.get("lowestPrice"))>max){
										max=Double.parseDouble(mt.get("lowestPrice"));
									}
									avg+=Double.parseDouble(mt.get("lowestPrice"));
									len+=1;
								}else if(DateUtil.compare_date(insertTime, mtLastQuarter)==0){
									avg+=Double.parseDouble(mt.get("lowestPrice"));
									len+=1;
									lmmStatistic.put("mtQuarterSame", Double.parseDouble(mt.get("lowestPrice")));
								}
							}
						}
					}
					lmmStatistic.put("mtQuarterMax",max );
					if(min!=Double.MAX_VALUE){
						lmmStatistic.put("mtQuarterMin", min);
					}

					if(len!=0){
						lmmStatistic.put("mtQuarterAvg", Double.parseDouble(df.format(avg/len)));
					}else{
						lmmStatistic.put("mtQuarterAvg", Double.parseDouble(df.format(0)));
					}

					/**
					 * 上个月的今天
					 */
					Date mtLastMonth=DateUtil.getDateBeforeMonth(mtNewGrabTime);
					/**
					 * 在所有数据中得到上个月的数据
					 */
					max=0;
					min=Double.MAX_VALUE;
					avg=0;
					len=0;
					for(int i=0;i<mtList.size();i++){
						Map<String, String> mt=mtList.get(i);
						if(mt!=null&&mt.get("createTime")!=null){
							Date insertTime=DateUtil.stringToDate(mt.get("createTime"),"yyyyMMdd");
							if(mt.get("lowestPrice")!=null&&mt.get("lowestPrice").length()>0&&StringTools.isDoubleNumeric(mt.get("lowestPrice"))){
								if(DateUtil.compare_date(insertTime, mtLastMonth)>0){
									if(Double.parseDouble(mt.get("lowestPrice"))<min){
										min=Double.parseDouble(mt.get("lowestPrice"));
									}
									if(Double.parseDouble(mt.get("lowestPrice"))>max){
										max=Double.parseDouble(mt.get("lowestPrice"));
									}
									avg+=Double.parseDouble(mt.get("lowestPrice"));
									len+=1;
								}else if(DateUtil.compare_date(insertTime, mtLastMonth)==0){
									avg+=Double.parseDouble(mt.get("lowestPrice"));
									len+=1;
									lmmStatistic.put("mtMonthSame", Double.parseDouble(mt.get("lowestPrice")));
								}
							}
						}
					}
					lmmStatistic.put("mtMonthMax",max );
					if(min!=Double.MAX_VALUE){
						lmmStatistic.put("mtMonthMin", min);
					}

					if(len!=0){
						lmmStatistic.put("mtMonthAvg", Double.parseDouble(df.format(avg/len)));
					}else{
						lmmStatistic.put("mtMonthAvg", Double.parseDouble(df.format(0)));
					}

				}
			}

		}








		model.addAttribute("statistics",lmmStatistic);
		
		/**
		 * 7日内新增商品、活跃商品数据、建议从MySQL中获得
		 */
		/**
		 * 1.7日内新增商品
		 * （1）先判断改产品是否为新增产品（若为新增则不需要再做其他判断）
		 *   根据id中时间做判断，跟商品日期做对比，若差值在7或7以内则为新增产品
		 * （2）若产品不为新增则判断商品是否为新增
		 * 	  根据new_grap_time进行判断，若差值在7或7以内则为新增商品
		 */
		List<LMMTicketCommodity> lNewCommodity=new ArrayList<LMMTicketCommodity>();
		List<XCTicketCommodityItem>xcNewCommodity=new ArrayList<XCTicketCommodityItem>();
		List<TNTicketCommodityItem>tnNewCommodity=new ArrayList<TNTicketCommodityItem>();
		List<TCommodityItem>tcNewCommodity=new ArrayList<TCommodityItem>();
		List<QNRTicketCommodityItem>qnrNewCommodity=new ArrayList<QNRTicketCommodityItem>();
		List<MTTicketCommodityItem>mtNewCommodity=new ArrayList<>();

		//不是新增产品，继续判断商品中是否有新增
		//lmm新增商品
		List<LMMTicketCommodity> comms=lmmCommodityService.selectByProdId(comm);
		for(int i=0;i<comms.size();i++){
			LMMTicketCommodity lcomm=comms.get(i);
			if(DateUtil.isSeven(7,lcomm.getCreateTime())&&lNewCommodity.size()<2){
				lNewCommodity.add(lcomm);
			}
			if(lNewCommodity.size()==2){
				break;
			}
		}
		//xc新增商品
		if(rp.getProdIdXC()!=null&&!"".equals(rp.getProdIdXC())){
			List<XCTicketCommodityItem> xcComms=xcCommodityService.selectByProdId(rp.getProdIdXC());
			for(int i=0;i<xcComms.size();i++){
				XCTicketCommodityItem xcComm=xcComms.get(i);
				
				if(DateUtil.isSeven(7,xcComm.getCreate_time())&&xcNewCommodity.size()<2){
					xcNewCommodity.add(xcComm);
				}
				if(xcNewCommodity.size()==2){
					break;
				}
			}
		}
		
		//tn新增商品
		if(rp.getProdIdTN()!=null&&!"".equals(rp.getProdIdTN())){
			List<TNTicketCommodityItem> tnComms=tnCommodityService.selectByProdId(rp.getProdIdTN());
			for(int i=0;i<tnComms.size();i++){
				TNTicketCommodityItem tnComm=tnComms.get(i);
				if(DateUtil.isSeven(7, tnComm.getCreate_time())&&tnNewCommodity.size()<2){
					tnNewCommodity.add(tnComm);
				}
				if(tnNewCommodity.size()==2){
					break;
				}
			}
		}
		
		//tc新增商品
		if(rp.getProdIdTC()!=null&&!"".equals(rp.getProdIdTC())){
			List<TCommodityItem> tcComms=tcCommodityService.selectByProdId(rp.getProdIdTC());
			for(int i=0;i<tcComms.size();i++){
				TCommodityItem tcComm=tcComms.get(i);
				if(DateUtil.isSeven(7, tcComm.getCreate_time())&&tcNewCommodity.size()<2){
					tcNewCommodity.add(tcComm);
				}
				if(tcNewCommodity.size()==2){
					break;
				}
			}
		}
		
		//qnr新增商品
		if(rp.getProdIdQNR()!=null&&!"".equals(rp.getProdIdQNR())){
			List<QNRTicketCommodityItem> qnrComms=qnrCommodityService.selectByProdId(rp.getProdIdQNR());
			for(int i=0;i<qnrComms.size();i++){
				QNRTicketCommodityItem qnrComm=qnrComms.get(i);
				if(DateUtil.isSeven(7, qnrComm.getCreate_time())&&qnrNewCommodity.size()<2){
					qnrNewCommodity.add(qnrComm);
				}
				if(qnrNewCommodity.size()==2){
					break;
				}
			}
		}
		//美团新增商品
		if(rp.getProdIdMT()!=null&&!"".equals(rp.getProdIdMT())){
			List<MTTicketCommodityItem> mtComms=mtCommodityService.selectByProdId(rp.getProdIdMT());
			if(mtComms!=null&&mtComms.size()>0){
				for(int i=0;i<mtComms.size();i++){
					MTTicketCommodityItem mtComm=mtComms.get(i);
					if(DateUtil.isSeven(7,mtComm.getCreate_time())&&mtNewCommodity.size()<2){
						mtNewCommodity.add(mtComm);
					}
					if(mtNewCommodity.size()==2){
						break;
					}
				}
			}
		}
			
		
		/**
		 * 活跃商品
		 */
		 Map<String,List> activeComms=new HashMap();
		 List<LMMTicketCommodity> lActiveComms=lmmCommodityService.getActiveCommodity(productId);
		 activeComms.put("lActiveComms", lActiveComms);
		 if(rp.getProdIdXC()!=null&&!"".equals(rp.getProdIdXC().trim())){
			 List<XCTicketCommodityItem> xcActiveComms=xcCommodityService.getActiveCommodity(rp.getProdIdXC());
			 activeComms.put("xcActiveComms", xcActiveComms);
		 }
		 
		 if(rp.getProdIdTN()!=null&&!"".equals(rp.getProdIdTN().trim())){
			 List<TNTicketCommodityItem> tnActiveComms=tnCommodityService.getActiveCommodity(rp.getProdIdTN());
			 activeComms.put("tnActiveComms", tnActiveComms);
		 }
		 if(rp.getProdIdTC()!=null&&!"".equals(rp.getProdIdTC().trim())){
			 List<TCommodityItem> tcActiveComms=tcCommodityService.getActiveCommodity(rp.getProdIdTC());
			 activeComms.put("tcActiveComms", tcActiveComms);
		 }
		 if(rp.getProdIdQNR()!=null&&!"".equals(rp.getProdIdQNR().trim())){
			 List<QNRTicketCommodityItem> qnrActiveComms=qnrCommodityService.getActiveCommodity(rp.getProdIdQNR());
			 activeComms.put("qnrActiveComms", qnrActiveComms);
		 }
		 //新增美团活跃商品
		 if(rp.getProdIdMT()!=null&&!"".equals(rp.getProdIdMT().trim())){
		 	List<MTTicketCommodityItem> mtActiveComms=mtCommodityService.getActiveCommodity(rp.getProdIdMT());
		 	activeComms.put("mtActiveComms",mtActiveComms);
		 }
		 
		model.addAttribute("activeComms", activeComms);
		/**
		 * 年季月度最高最低平均值、产品历史价格、图片丰富度、商品丰富度、
		 * 商品打包率、商品优惠率、商品类型丰富度从HBase中查询
		 */
		
		Map<String,List> newCommodity=new HashMap();
		newCommodity.put("lNewCommodity", lNewCommodity);
		newCommodity.put("xcNewCommodity", xcNewCommodity);
		newCommodity.put("tnNewCommodity", tnNewCommodity);
		newCommodity.put("tcNewCommodity", tcNewCommodity);
		newCommodity.put("qnrNewCommodity", qnrNewCommodity);
		newCommodity.put("mtNewCommodity",mtNewCommodity);
		model.addAttribute("newCommodity", newCommodity);
		
		return "/datashow/screenDemo";
	}
	/**
	 * 产品历史价格等图表内容
	 * @param productId
	 * @return
	 */
	@RequestMapping("/statisticsList")
	@ResponseBody
	public Map<String, Object> getStatList(String productId,String businessCode){
		
		Map<String, Object> result=getResult();
		List<LineDataVO> priceList = new ArrayList<>();
		List<LineDataVO> imageNumList = new ArrayList<>();
		List<LineDataVO> commNumList = new ArrayList<>();
		List<LineDataVO> commTypeNumList = new ArrayList<>();
		List<LineDataVO> preNumList = new ArrayList<>();
		List<LineDataVO> packNumList = new ArrayList<>();
		List<LineDataVO> prodScoreList = new ArrayList<>();
		CommodityConditionVO comm=getComm();
		comm.setProductId(productId);
		RelevanceProduct  rp=relevanceProductService.viewRelevance(productId,businessCode);
		String tableNamel=Constant.TICKET_PRODUCT_H_LMM;
		String family=Constant.HB_VARIABLE_FAMILY;
		LMMTicketProduct lp=lProductService.selectByProductId(productId);
		List<Map<String,String>> lmmList=historyPrice.queryByProductId(rp.getProdId(), tableNamel,family,lp.getNewGrabTime());//根据产品ID，表名查询
		Collections.sort(lmmList,new Comparator<Map<String,String>>() {
			@Override
			public int compare(Map<String, String> o1, Map<String, String> o2) {
//				return o1.get("insert_time").compareTo(o2.get("insert_time"));
				if(o1==null||StringUtils.isEmpty(o1.get("insert_time")))//排除空指针
					return -1;
				if(o2==null||StringUtils.isEmpty(o2.get("insert_time")))
					return 1;
				return o1.get("insert_time").compareTo(o2.get("insert_time"));
			}
			
		});

		//封装产品历史价格数据
		parseLineDataVo(lmmList,"lmm",priceList,imageNumList,commNumList,preNumList,packNumList,commTypeNumList,prodScoreList);
		if(rp.getProdIdXC()!=null&&!"".equals(rp.getProdIdXC().trim())){
			XCTicketProduct xcp=xcProductService.selectByProdId(rp.getProdIdXC());
			if(xcp!=null){
				String tableNamex=Constant.TICKET_PRODUCT_H_XC;
				List<Map<String,String>> xcList=historyPrice.queryByProductId(rp.getProdIdXC(), tableNamex,family,xcp.getCreateTime());
				Collections.sort(xcList,new Comparator<Map<String,String>>() {
					@Override
					public int compare(Map<String, String> o1, Map<String, String> o2) {
//						return o1.get("create_time").compareTo(o2.get("create_time"));
						if(o1==null||StringUtils.isEmpty(o1.get("create_time")))//排除空指针
							return -1;
						if(o2==null||StringUtils.isEmpty(o2.get("create_time")))
							return 1;
						return o1.get("create_time").compareTo(o2.get("create_time"));
					}
					
				});
				parseLineDataVo(xcList,"xc",priceList,imageNumList,commNumList,preNumList,packNumList,commTypeNumList,prodScoreList);
			}
			
		}
		//新增美团业务逻辑
		if(rp.getProdIdMT()!=null &&!"".equals(rp.getProdIdMT().trim())){
			MTTicketProduct mtp=mtProductService.selectByProdId(rp.getProdIdMT());//根据产品ID查找对应的美团产品
			if(mtp!=null){
//				String tableNamemt=Constant.TICKET_PRODUCT_H_QNR;//暂无美团数据，暂时使用去哪儿历史表
				String tableNamemt=Constant.TICKET_PRODUCT_H_MT;//查询美团历史价格表
//				List<Map<String,String>> mtList=historyPrice.queryByProductId(rp.getProdIdQNR(),tableNamemt,family,mtp.getCreateTime());//根据产品ID查询历史记录
				List<Map<String,String>> mtList=historyPrice.queryByProductId(rp.getProdIdMT(),tableNamemt,family,mtp.getCreateTime());//根据产品ID查询历史记录
				Collections.sort(mtList, new Comparator<Map<String, String>>() {
					@Override
					public int compare(Map<String, String> o1, Map<String, String> o2) {
						if(o1==null || StringUtils.isEmpty(o1.get("createTime")))
							return -1;
						if(o2==null || StringUtils.isEmpty(o2.get("createTime")))
							return 1;
						return o1.get("createTime").compareTo(o2.get("createTime"));
					}
				});
				if(mtList!=null){
					parseLineDataVo(mtList,"mt",priceList,imageNumList,commNumList,preNumList,packNumList,commTypeNumList,prodScoreList);
				}
			}

		}
		
		if(rp.getProdIdTN()!=null&&!"".equals(rp.getProdIdTN().trim())){
			
			TNTicketProduct tnp=tnProductService.selectByProdId(rp.getProdIdTN());
			if(tnp!=null){
				/**
				 * 根据productId拿到tn历史列表并进行排序
				 */
				String tableNamen=Constant.TICKET_PRODUCT_H_TN;
				List<Map<String,String>> tnList=historyPrice.queryByProductId(rp.getProdIdTN(), tableNamen,family,tnp.getCreateTime());
				Collections.sort(tnList,new Comparator<Map<String,String>>() {
					@Override
					public int compare(Map<String, String> o1, Map<String, String> o2) {
						if(o1==null||StringUtils.isEmpty(o1.get("create_time")))
							return -1;
						if(o2==null||StringUtils.isEmpty(o2.get("create_time")))
							return 1;
						return o1.get("create_time").compareTo(o2.get("create_time"));

					}
					
				});
				parseLineDataVo(tnList,"tn",priceList,imageNumList,commNumList,preNumList,packNumList,commTypeNumList,prodScoreList);
			}
			
		}
		if(rp.getProdIdTC()!=null&&!"".equals(rp.getProdIdTC().trim())){
			TCTicketProduct tcp=tcProductService.selectByProdId(rp.getProdIdTC());
			if(tcp!=null){
				String tableNamec=Constant.TICKET_PRODUCT_H_TC;
				List<Map<String,String>> tcList=historyPrice.queryByProductId(rp.getProdIdTC(), tableNamec,family,tcp.getNewGrabTime());
				Collections.sort(tcList,new Comparator<Map<String,String>>() {
					@Override
					public int compare(Map<String, String> o1, Map<String, String> o2) {
						if(o1==null||StringUtils.isEmpty(o1.get("create_time")))
							return -1;
						if(o2==null||StringUtils.isEmpty(o2.get("create_time")))
							return 1;
						return o1.get("create_time").compareTo(o2.get("create_time"));
					}
				});
				parseLineDataVo(tcList,"tc",priceList,imageNumList,commNumList,preNumList,packNumList,commTypeNumList,prodScoreList);
			}
			
		}
		if(rp.getProdIdQNR()!=null&&!"".equals(rp.getProdIdQNR().trim())){
			QNRTicketProduct qnrp=qnrProductService.selectByProdId(rp.getProdIdQNR());
			if(qnrp!=null){
				String tableNameq=Constant.TICKET_PRODUCT_H_QNR;
				List<Map<String,String>> qnrList=historyPrice.queryByProductId(rp.getProdIdQNR(), tableNameq,family,qnrp.getCreateTime());
				Collections.sort(qnrList,new Comparator<Map<String,String>>() {
					@Override
					public int compare(Map<String, String> o1, Map<String, String> o2) {
						if(o1==null||StringUtils.isEmpty(o1.get("create_time")))
							return -1;
						if(o2==null||StringUtils.isEmpty(o2.get("create_time")))
							return 1;
						return o1.get("create_time").compareTo(o2.get("create_time"));
					}
				});
				parseLineDataVo(qnrList,"qnr",priceList,imageNumList,commNumList,preNumList,packNumList,commTypeNumList,prodScoreList);
			}
			
		}
		JSONArray priceData = JSONArray.fromObject(priceList);
		JSONArray imageNumData = JSONArray.fromObject(imageNumList);
		JSONArray commNumData = JSONArray.fromObject(commNumList);
		JSONArray commTypeNumData = JSONArray.fromObject(commTypeNumList);
		JSONArray preNumData = JSONArray.fromObject(preNumList);
		JSONArray packNumData = JSONArray.fromObject(packNumList);
		JSONArray prodScoreData = JSONArray.fromObject(prodScoreList);
		result.put("priceData", priceData);
		result.put("imageNumData", imageNumData);
		result.put("commNumData", commNumData);
		result.put("commTypeNumData", commTypeNumData);
		result.put("preNumData", preNumData);
		result.put("packNumData", packNumData);
		result.put("prodScoreData", prodScoreData);
		return result;
	}
	public Map<String, Object> getResult() {
    	
        Map<String, Object> result = new HashMap<String, Object>();
        
        return result;
    }
	public CommodityConditionVO getComm(){
		
		return new CommodityConditionVO();
	}
	/**
	 * @Title: parseJsonForLine
	 * @Description: 折线图数据：map封装对象转json数组
	 * @param @param mapForLine
	 * @param @return 设定文件
	 * @return JSONArray 返回类型
	 * @throws
	 */
	private void parseLineDataVo(List<Map<String, String>> map,String companyCode,List<LineDataVO>priceList,
			List<LineDataVO>imageNumList,List<LineDataVO>commNumList,List<LineDataVO>preNumList,
			List<LineDataVO>packNumList,List<LineDataVO>commTypeNumList,List<LineDataVO>prodScoreList) {
		DecimalFormat    df   = new DecimalFormat("######0.0");
		for(Map<String,String> data:map){
			String create_time="";
			if(data!=null){
				if("lmm".equals(companyCode)){
					if(data.get("insert_time")!=null&&data.get("insert_time").length()>=8)
					create_time=data.get("insert_time").substring(0, 4)+"-"+data.get("insert_time").substring(4, 6)+"-"+data.get("insert_time").substring(6, 8);
				}else{
					if(data.get("create_time")!=null&&data.get("create_time").length()>=8)
					create_time=data.get("create_time").substring(0, 4)+"-"+data.get("create_time").substring(4, 6)+"-"+data.get("create_time").substring(6, 8);
					//由于美团数据结构不同，暂时新增以下臃肿逻辑，期以后重构
					if("mt".equals(companyCode)&&data.get("createTime")!=null&&data.get("createTime").length()>=8)
					{
						create_time=data.get("createTime").substring(0, 4)+"-"+data.get("createTime").substring(4, 6)+"-"+data.get("createTime").substring(6, 8);
					}

				}

				//由于美团使用的数据结构不同，暂时新增以下臃肿逻辑，期以后重构
				if("mt".equals(companyCode))
				{
					if(data.get("lowestPrice")!=null&&!"null".equals(data.get("lowestPrice"))){
						LineDataVO price = new LineDataVO();

						price.setTime(create_time);
						price.setCategory(companyCode);
						price.setAmount(Double.valueOf(data.get("lowestPrice")==null?"":data.get("lowestPrice")));
						priceList.add(price);
					}
					if(data.get("imageNum")!=null&&!"null".equals(data.get("imageNum"))){
						LineDataVO imageNum = new LineDataVO();
						imageNum.setTime(create_time);
						imageNum.setCategory(companyCode);
						imageNum.setAmount(Double.valueOf(data.get("imageNum")==null?"":data.get("imageNum")));
						imageNumList.add(imageNum);
					}
					if(data.get("commodityNum")!=null&&!"null".equals(data.get("commodityNum"))){
						LineDataVO commNum = new LineDataVO();
						commNum.setTime(create_time);
						commNum.setCategory(companyCode);
						commNum.setAmount(Double.valueOf(data.get("commodityNum")));
						commNumList.add(commNum);
					}
					if(data.get("commTypeNum")!=null&&!"null".equals(data.get("commTypeNum"))){
						LineDataVO commTypeNum = new LineDataVO();
						commTypeNum.setTime(create_time);
						commTypeNum.setCategory(companyCode);
						commTypeNum.setAmount(Double.valueOf(data.get("commTypeNum")));
						commTypeNumList.add(commTypeNum);
					}
					if(data.get("commPreferentialNum")!=null&&data.get("commodityNum")!=null&&!"0".equals(data.get("commodityNum"))&&!"null".equals(data.get("commodityNum"))&&!"null".equals(data.get("commPreferentialNum"))){
						LineDataVO preNum = new LineDataVO();
						preNum.setTime(create_time);
						preNum.setCategory(companyCode);
						preNum.setAmount(Double.parseDouble(df.format(Double.valueOf(data.get("commPreferentialNum"))/Double.valueOf(data.get("commodityNum")))));
						preNumList.add(preNum);
					}
					if(data.get("commPackageNum")!=null&&data.get("commodityNum")!=null&&!"0".equals(data.get("commodityNum"))&&!"null".equals(data.get("commodityNum"))&&!"null".equals(data.get("commPackageNum"))){
						LineDataVO packNum = new LineDataVO();
						packNum.setTime(create_time);
						packNum.setCategory(companyCode);
						packNum.setAmount(Double.parseDouble(df.format(Double.valueOf(data.get("commPackageNum"))/Double.valueOf(data.get("commodityNum")))));
						packNumList.add(packNum);
					}
					if(data.get("score")!=null&&!"null".equals(data.get("score"))){
						LineDataVO product_grade = new LineDataVO();
						product_grade.setTime(create_time);
						product_grade.setCategory(companyCode);
						try{
							product_grade.setAmount(Double.parseDouble(df.format(Double.valueOf(data.get("score")))));//此处的值可能产生乱码导致无法解析成Double类型
						}catch (NumberFormatException e){
							product_grade.setAmount(0.0);//当数据为异常时设置为0.0
						}
						prodScoreList.add(product_grade);
					}

				}

				if(data.get("lowest_price")!=null&&!"null".equals(data.get("lowest_price"))){
					LineDataVO price = new LineDataVO();

					price.setTime(create_time);
					price.setCategory(companyCode);
					price.setAmount(Double.valueOf(data.get("lowest_price")==null?"":data.get("lowest_price")));
					priceList.add(price);
				}
				if(data.get("image_num")!=null&&!"null".equals(data.get("image_num"))){
					LineDataVO imageNum = new LineDataVO();
					imageNum.setTime(create_time);
					imageNum.setCategory(companyCode);
					imageNum.setAmount(Double.valueOf(data.get("image_num")==null?"":data.get("image_num")));
					imageNumList.add(imageNum);
				}
				if(data.get("commodity_num")!=null&&!"null".equals(data.get("commodity_num"))){
					LineDataVO commNum = new LineDataVO();
					commNum.setTime(create_time);
					commNum.setCategory(companyCode);
					commNum.setAmount(Double.valueOf(data.get("commodity_num")));
					commNumList.add(commNum);
				}
				if(data.get("comm_type_num")!=null&&!"null".equals(data.get("comm_type_num"))){
					LineDataVO commTypeNum = new LineDataVO();
					commTypeNum.setTime(create_time);
					commTypeNum.setCategory(companyCode);
					commTypeNum.setAmount(Double.valueOf(data.get("comm_type_num")));
					commTypeNumList.add(commTypeNum);
				}
				if(data.get("comm_preferential_num")!=null&&data.get("commodity_num")!=null&&!"0".equals(data.get("commodity_num"))&&!"null".equals(data.get("commodity_num"))&&!"null".equals(data.get("comm_preferential_num"))){
					LineDataVO preNum = new LineDataVO();
					preNum.setTime(create_time);
					preNum.setCategory(companyCode);
					preNum.setAmount(Double.parseDouble(df.format(Double.valueOf(data.get("comm_preferential_num"))/Double.valueOf(data.get("commodity_num")))));
					preNumList.add(preNum);
				}
				if(data.get("comm_package_num")!=null&&data.get("commodity_num")!=null&&!"0".equals(data.get("commodity_num"))&&!"null".equals(data.get("commodity_num"))&&!"null".equals(data.get("comm_package_num"))){
					LineDataVO packNum = new LineDataVO();
					packNum.setTime(create_time);
					packNum.setCategory(companyCode);
					packNum.setAmount(Double.parseDouble(df.format(Double.valueOf(data.get("comm_package_num"))/Double.valueOf(data.get("commodity_num")))));
					packNumList.add(packNum);
				}
				if(data.get("product_grade")!=null&&!"null".equals(data.get("product_grade"))){
					LineDataVO product_grade = new LineDataVO();
					product_grade.setTime(create_time);
					product_grade.setCategory(companyCode);
					try{
						product_grade.setAmount(Double.parseDouble(df.format(Double.valueOf(data.get("product_grade")))));//此处的值可能产生乱码导致无法解析成Double类型
					}catch (NumberFormatException e){
						product_grade.setAmount(0.0);//当数据为异常时设置为0.0
					}
					prodScoreList.add(product_grade);
				}
			}

			
		}
	}


}
